//
//  LTViewController.m
//  LiveInterfaceDemo
//
//  Created by 邓猛 on 2017/10/30.
//  Copyright © 2017年 dim. All rights reserved.
//

#import "LTViewController.h"
#import "LIITableViewCell.h"
#import "LSActionSheet.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "CoreWork.h"
#import "RequestWork.h"

@interface LTViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *titles;


@end

@implementation LTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-100) style:UITableViewStylePlain];
    self.tableView.delegate= self;
    self.tableView.dataSource = self;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
    self.tableView.sectionFooterHeight = 10;
    self.titles = @[@"11月1号",@"11月2号",@"6月6号",@"7月7号",@"8月8号"];
    [self.view addSubview:self.tableView];
   
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
    }];
    //或
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(Actiomm)];
  
    
}

- (void)Actiomm{
    [self.tableView.mj_header beginRefreshing];
    NSLog(@"刷新了");
    [self.tableView.mj_header endRefreshing];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
    backView.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, self.view.bounds.size.width-20, 40)];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont boldSystemFontOfSize:18];
    label.text = self.titles[section];
    [backView addSubview:label];
    return backView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LIITableViewCell *cell = [[NSBundle mainBundle]loadNibNamed:@"LIITableViewCell" owner:nil options:nil].firstObject;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0){
        if (indexPath.row == 0){
            [cell.ViedioButton addTarget:self action:@selector(ViedioAction) forControlEvents:UIControlEventTouchUpInside];
            [cell.ChooseButton addTarget:self action:@selector(explace:) forControlEvents:UIControlEventTouchUpInside];
            [cell.ChooseButton setBackgroundImage:[UIImage imageNamed:@"33"] forState:UIControlStateNormal];
        }else{
//            cell.ViedioButton.userInteractionEnabled = NO;
//            cell.ViedioButton.alpha = 0.4;
            [cell.ChooseButton setBackgroundImage:[UIImage imageNamed:@"22"] forState:UIControlStateNormal];
            [cell.ChooseButton addTarget:self action:@selector(chooseOnlyOne:) forControlEvents:UIControlEventTouchUpInside];
            [cell.ChooseButton setBackgroundImage:[UIImage imageNamed:@"12"] forState:UIControlStateSelected];
             [cell.ViedioButton addTarget:self action:@selector(UnViedioAction) forControlEvents:UIControlEventTouchUpInside];
        }
    }else{
         [cell.ViedioButton addTarget:self action:@selector(ViedioAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.ChooseButton setBackgroundImage:[UIImage imageNamed:@"22"] forState:UIControlStateNormal];
        [cell.ChooseButton addTarget:self action:@selector(chooseOnlyOne:) forControlEvents:UIControlEventTouchUpInside];
        [cell.ChooseButton setBackgroundImage:[UIImage imageNamed:@"12"] forState:UIControlStateSelected];
    }
    return cell;
}

- (void)UnViedioAction{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.bezelView.color = [UIColor blackColor];
    hud.label.textColor = [UIColor whiteColor];
    hud.label.text = @"暂时没有线路哎╮(╯▽╰)╭";
    hud.margin = 10.f;
    hud.yOffset = 10.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:0.8f];
}

- (void)ViedioAction{
    [LSActionSheet showWithTitle:nil destructiveTitle:nil otherTitles:@[@"CCTV",@"铁岭TV"] block:^(int index) {
        UIAlertController *alert11 = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
        if(index == 0){
            UIAlertAction *ca =  [UIAlertAction actionWithTitle:@"嗷呜" style:UIAlertActionStyleDefault handler:nil];
            [alert11 addAction:ca];
            NSLog(@"感谢CCTV");
        }else{
            UIAlertAction *sureQ = [UIAlertAction actionWithTitle:@"欧虎" style:UIAlertActionStyleDefault handler:nil];
            [alert11 addAction:sureQ];
            NSLog(@"感激铁岭TV");
        }
        [self presentViewController:alert11 animated:YES completion:nil];
    }];
}

- (void)explace:(UIButton *)btn{
    [LSActionSheet showWithTitle:nil destructiveTitle:nil otherTitles:@[@"CCTV",@"铁岭TV"] block:^(int index) {
        UIAlertController *alert11 = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
        if(index == 0){
            NSString * urlStr = @"https://www.baidu.com";
            NSURL *url = [NSURL URLWithString:urlStr];
            if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
                if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
                    [[UIApplication sharedApplication] openURL:url options:@{}
                                             completionHandler:^(BOOL success) {
                                                 NSLog(@"Open %d",success);
                                             }];
                } else {
                    BOOL success = [[UIApplication sharedApplication] openURL:url];
                    NSLog(@"Open  %d",success);
                }
                
            } else{
                bool can = [[UIApplication sharedApplication] canOpenURL:url];
                if(can){
                    [[UIApplication sharedApplication] openURL:url];
                }
            }
            UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"CCTV" style:UIAlertActionStyleDefault handler:nil];
            [alert11 addAction:OKAction];
//            NSLog(@"感谢CCTV");4
        }else{
            // http://baishi.baidu.com/watch/04089433700701852999.html?page=videoMultiNeed
            NSString * urlStr = @"http://baishi.baidu.com/watch/04089433700701852999.html?page=videoMultiNeed";
            NSURL *url = [NSURL URLWithString:urlStr];
            if([[UIDevice currentDevice].systemVersion floatValue] >= 10.0){
                if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
                    [[UIApplication sharedApplication] openURL:url options:@{}
                                             completionHandler:^(BOOL success) {
                                                 NSLog(@"Open %d",success);
                                             }];
                } else {
                    BOOL success = [[UIApplication sharedApplication] openURL:url];
                    NSLog(@"Open  %d",success);
                }
                
            } else{
                bool can = [[UIApplication sharedApplication] canOpenURL:url];
                if(can){
                    [[UIApplication sharedApplication] openURL:url];
                }
            }
        }
    }];
}

- (void)chooseOnlyOne:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if(btn.selected){
        NSLog(@"预定成功");
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        hud.label.text = @"预约成功";
        [hud hideAnimated:YES afterDelay:0.8f];
    }else{
        NSLog(@"预定");
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeCustomView;
        UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        hud.customView = [[UIImageView alloc] initWithImage:image];
        hud.square = YES;
        hud.label.text = @"取消预定";
        [hud hideAnimated:YES afterDelay:0.8f];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





@end
